package com.example.zq;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * @author : Zhang Qi
 * @date : 2019/11/25
 */
@RestController
public class UserController {
  private Map<Integer,User> userMap=new HashMap<>();

  @DeleteMapping("/user/{id}")
  public void deleteUserById(@PathVariable Integer id) {
    userMap.remove(id);
  }
  @GetMapping("/user")
  public User getUserById(@RequestParam Integer id) {
    return userMap.get(id);
  }
  @PostMapping("/user")
  public User addUser(@RequestBody User user) {
    int id = user.getId();
    userMap.put(id,user);
    return userMap.get(id);
  }
  @PutMapping("/user")
  public void updateUserById(@RequestHeader String name, @RequestHeader Integer id) {
    User user = userMap.get(id);
    user.setUsername(name);
  }
}
